package com.elric.mall.controller;

import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.common.utils.DateUtils;
import com.elric.mall.common.utils.ExcelFileUtil;
import com.elric.mall.dto.ProgrammeManageDto;
import com.elric.mall.dto.ProgrammeProjectDto;
import com.elric.mall.dto.ProgrammeProjectExportDto;
import com.elric.mall.dto.ProgrammeProjectImportDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.model.Dictionary;
import com.elric.mall.model.ProgrammeProject;
import com.elric.mall.model.UmsAdmin;
import com.elric.mall.service.ProgrammeProjectService;
import com.elric.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实现功能：计划项目控制层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "ProgrammeProjectController", description = "计划项目接口")
@RequestMapping("/programmeProject")
@RestController
public class ProgrammeProjectController extends BaseController {

    @Autowired
    ProgrammeProjectService programmeProjectService;

    @Autowired
    private UserService userService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody ProgrammeProject programmeProject) {
        int count = programmeProjectService.create(programmeProject);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody ProgrammeManageDto programmeProject) {
        int count = programmeProjectService.update(programmeProject);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("保存项目信息")
    @RequestMapping(value = "/saveProjectInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult saveProjectInfo(@RequestBody ProgrammeProjectDto dto, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        return CommonResult.success(programmeProjectService.save(dto, umsAdmin));
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ProgrammeProjectDto> getItem(@PathVariable String id) {
        ProgrammeProjectDto ProgrammeProject = programmeProjectService.findById(id);
        return CommonResult.success(ProgrammeProject);
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable String id) {
        int count = programmeProjectService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<ProgrammeProject>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestBody ProgrammeProject programmeProject) {
        List<ProgrammeProject> menuList = programmeProjectService.list(pageSize, pageNum, programmeProject);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("分页查询计划项目治理数据")
    @RequestMapping(value = "/findManageByPage", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<ProgrammeManageDto>> findManageByPage(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestBody ProgrammeManageDto programmeProject) {
        List<ProgrammeManageDto> menuList = programmeProjectService.findManageByPage(pageSize, pageNum, programmeProject);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("计划项目治理查询统计")
    @RequestMapping(value = "/manageStatistics", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult manageStatistics(@RequestBody ProgrammeManageDto programmeProject) {
        return CommonResult.success(programmeProjectService.manageStatistics(programmeProject));
    }

    /**
     * excel导入
     *
     * @param request 请求参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/importDocuments", method = RequestMethod.POST)
    @ApiOperation(value = "固定表导入", notes = "固定表导入")
    public CommonResult importDocuments(MultipartHttpServletRequest request) {
        return CommonResult.success(programmeProjectService.importDocuments(request));
    }

    @ApiOperation("异构excel批量导入")
    @RequestMapping(value = "/importHDocuments", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult importHDocuments(MultipartHttpServletRequest request) {
        return CommonResult.success(programmeProjectService.importHDocuments(request));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<ProgrammeProject> list) {
        return CommonResult.success(programmeProjectService.insertBatch(list, type));
    }

    @ApiOperation("批量导入（计划项目信息主表以及相关拓展表）")
    @RequestMapping(value = "/insertExcelBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertExcelBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                         @RequestBody List<ProgrammeProjectImportDto> list, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        return CommonResult.success(programmeProjectService.insertExcelBatch(list, umsAdmin, type));
    }

    @ApiOperation("任务数据导入（计划项目信息主表以及相关拓展表）")
    @RequestMapping(value = "/insertTaskBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertTaskBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                        @RequestParam(value = "taskUnitId") String taskUnitId,
                                         @RequestBody List<ProgrammeProjectImportDto> list, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        programmeProjectService.insertTaskBatch(list, umsAdmin, type, taskUnitId);
        return CommonResult.success("ok");
    }

    @ApiOperation("获取导入模板")
    @RequestMapping(value = "/getImportTemplate", method = RequestMethod.GET)
    @ResponseBody
    public void getImportTemplate(HttpServletResponse response) {
        ExcelFileUtil.toExcel(response, new ArrayList<>(), "计划项目导入模板",new ProgrammeProjectImportDto());
    }

    @ApiOperation("数据治理")
    @RequestMapping(value = "/validateData", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult validateData() {
        programmeProjectService.validateData();
        return CommonResult.success("ok");
    }

    @ApiOperation("根据任务-大单位ID获取项目清单")
    @RequestMapping(value = "/findProjectByTaskUnitId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findProjectByTaskUnitId(@RequestParam(value = "id") String id) {
        return CommonResult.success(programmeProjectService.findProjectByTaskUnitId(id));
    }

    @ApiOperation("计划项目数据导出")
    @RequestMapping(value = "/exportProject", method = RequestMethod.POST)
    @ResponseBody
    public void exportProject(@RequestBody ProgrammeProject programmeProject, HttpServletResponse response) {
        List<ProgrammeProjectExportDto> list = programmeProjectService.findExportByModel(programmeProject);
        String fileName = "计划项目报表"+DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDDHH);
        ExcelFileUtil.toExcelWithXlsx(response, list, fileName,new ProgrammeProjectExportDto());
    }
}
